package com.example.onion.hxchat.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.onion.hxchat.util.MyLocalDatabaseHelper;
import com.example.superyi.myteam.R;
import com.hyphenate.EMContactListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;


/**
 * 填写申请好友验证信息，管理好友申请功能
 * 给出用户名userName，userId(统一加前缀apply)
 * applyUserId = savedInstanceState.getString("applyUserId");
 * applyUserName = savedInstanceState.getString("applyUserName");
 * 含version
 */
public class ApplyFriendActivity extends AppCompatActivity implements View.OnClickListener {

    private int version = 4;
    private EditText applyFriendEt;


    private TextView applyUserNameTv;

    private Toolbar toolbar;

    private final String TAG = "ApplyFriendActivity";
    private String applyUserName;//用户昵称
    private String applyUserId;//用户id
    private String applyUserSpy;
    private TextView applyUserSpyTv;

    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        outState.putString("userId", applyUserId);
        outState.putString("userName", applyUserName);
        super.onSaveInstanceState(outState);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            applyUserId = savedInstanceState.getString("userId");
            Log.e(TAG,"Bundle 保存的 userId"+applyUserId);
            applyUserName = savedInstanceState.getString("userName");
            Log.e(TAG,"Bundle 保存的 userName"+applyUserName);
        }
        setContentView(R.layout.activity_apply_friend);
        initView();

    }
    /**
     * 判断是否已经存在好友关系
     * @param userId id
     * @return 已经保存好友关系返回true,否则false
     */
    private boolean isExistFriend(String userId){
        MyLocalDatabaseHelper dbHelper = new MyLocalDatabaseHelper(ApplyFriendActivity.this,"local.db",null,version);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        @SuppressLint("Recycle") Cursor cursor = db.rawQuery("select userId from friend where userId = ?",new String[]{userId});
        return cursor.moveToFirst();
    }
    /**
     * 初始化部件
     */
    private void initView(){
        //传递给验证码输入页面的用户名称（最好根据需要传递到的不同的页面，命名kEY，此处需要传递到申请加好友界面，加前缀apply+UserName）
        applyUserName = getIntent().getStringExtra("applyUserName");
        applyUserId = getIntent().getStringExtra("applyUserId");
        applyUserSpy = getIntent().getStringExtra("applyUserSpy");
        Log.e("name", applyUserName);
        Log.e("id", applyUserId);
        applyFriendEt = findViewById(R.id.apply_friend_et);
        applyUserNameTv = findViewById(R.id.apply_user_name);
        applyUserSpyTv = findViewById(R.id.apply_user_spy);
        applyUserSpyTv.setText(applyUserSpy);
        applyUserNameTv.setText(applyUserName);
        applyFriendEt.setOnClickListener(this);
        toolbar = findViewById(R.id.apply_toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_apply_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home://系统自带的返回箭头的资源id
                finish();
                Log.d("ApplyActivity", "返回键");
                break;
            case  R.id.toolbar_apply_send:
                String applyInfo = applyFriendEt.getText().toString();
                //参数为要添加的好友的userId和添加理由
//                if (isExistFriend(applyUserId)){
//                    Toast.makeText(ApplyFriendActivity.this,"已互为好友，请勿重复操作！",Toast.LENGTH_SHORT).show();
//                    finish();
//                    break;
//                }
                try {
                    EMClient.getInstance().contactManager().addContact(applyUserId,applyInfo);
                } catch (HyphenateException e) {
                    Toast.makeText(ApplyFriendActivity.this,applyInfo+"发送失败",Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
                Toast.makeText(ApplyFriendActivity.this,applyInfo+"发送成功",Toast.LENGTH_SHORT).show();
                finish();
                break;
            default:
                break;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {

            default:
                break;
        }

    }



}
